package com.cloudlibrary.mapper;

import com.cloudlibrary.domain.Book;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

/**
* @author 30590
* @description 针对表【book】的数据库操作Mapper
* @createDate 2024-12-12 10:20:06
* @Entity com.cloudlibrary.domain.Book
*/
@Component
public interface BookMapper {
    @Select("select * from book where book_status !='3' order by book_uploadtime desc ")
    @Results(id = "bookMap",value = {
            @Result(id = true,column = "book_id",property = "id"),
            @Result(column = "book_name",property = "name"),
            @Result(column = "book_isbn",property = "isbn"),
            @Result(column = "book_press",property = "press"),
            @Result(column = "book_author",property = "author"),
            @Result(column = "book_pagination",property = "pagination"),
            @Result(column = "book_price",property = "price"),
            @Result(column = "book_uploadtime",property = "uploadtime"),
            @Result(column = "book_status",property = "status"),
            @Result(column = "book_borrower",property = "borrower"),
            @Result(column = "book_borrowtime",property = "borrowtime"),
            @Result(column = "book_returntime",property = "returntime"),
    })
    Page<Book> selectNewBooks();
    @Select("select * from book where book_id=#{id}")
    @ResultMap("bookMap")
    Book findById(String id);
    @Update({
            "<script>",
            "update book ",
            "<trim prefix=\"set\" suffixOverrides=\",\">",
            "<if test=\"name != null\">",
            "book_name= #{name},",
            "</if>",
            "<if test=\"isbn != null\">",
            "book_isbn= #{isbn},",
            "</if>",
            "<if test=\"press != null\">",
            "book_press= #{press},",
            "</if>",
            "<if test=\"author != null\">",
            "book_author= #{author},",
            "</if>",
            "<if test=\"pagination != null\">",
            "book_pagination= #{pagination},",
            "</if>",
            "<if test=\"price != null\">",
            "book_price= #{price},",
            "</if>",
            "<if test=\"uploadtime != null\">",
            "book_uploadtime= #{uploadtime},",
            "</if>",
            "<if test=\"status != null\">",
            "book_status= #{status},",
            "</if>",
            "<if test=\"borrower != null\">",
            "book_borrower= #{borrower},",
            "</if>",
            "<if test=\"borrowtime != null\">",
            "book_borrowtime= #{borrowtime},",
            "</if>",
            "<if test=\"returntime != null\">",
            "book_returntime= #{returntime},",
            "</if>",
            "</trim>",
            "where book_id = #{id}",
            "</script>"
    })
    @ResultMap("bookMap")//数据库字段和Java类属性
    Integer editBook(Book book);
    @Select({
            "<script>",
            "select * from book "+
            "where book_status !='3'"+
            "<if test=\"name !=null\"> and book_name like concat('%','{name}','%') </if>" +
            "<if test=\"press !=null\">  and book_press like concat('%','{press}','%') </if>" +
            "<if test=\"author !=null\"> and book_author like concat('%','{author}','%') </if>" +
            "order by book_status" +
            "</script>"
    })
    @ResultMap("bookMap")//数据库字段和Java类属性
    Page<Book> searchBooks(Book book);

    @Insert("insert into book(book_id,book_name,book_isbn,book_press,book_author,book_pagination," +
            "book_price,book_uploadtime,book_status,book_borrower, book_borrowtime,book_returntime)" +
            " values(#{id},#{name},#{isbn},#{press},#{author},#{pagination},#{price},#{uploadtime}," +
            "#{status},#{borrower},#{borrowtime},#{returntime})")
    Integer addBook(Book book);

    @Select({
            "<script>"+
            "select * from book "+
            "where book_borrower=#{borrower}"+
            "and book_status = '1'" +
            "<if test=\"name != null\"> " +
                    "and book_name like concat('%','{name}','%') </if>" +
            "<if test=\"press != null\"> " +
                    "and book_press like concat('%','{press}','%') </if>" +
            "<if test=\"author != null\"> " +
                    "and book_author like concat('%','{author}','%') </if>" +
            "or book_status ='2'"+
            "<if test=\"name != null\"> " +
                    "and book_name like concat('%','{name}','%') </if>" +
            "<if test=\"press != null\"> " +
                    "and book_press like concat('%','{press}','%') </if>" +
            "<if test=\"author != null\"> " +
                    "and book_author like concat('%','{author}','%') </if>" +
                    "order by book_borrower"+
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectBorrowed(Book book);
    @Select({
            "<script>"+
                    "select * from book "+
                    "where book_borrower=#{borrower}"+
                    "and book_status in('1','2')" +
                    "<if test=\"name != null\"> " +
                    "and book_name like concat('%','{name}','%') </if>" +
                    "<if test=\"press != null\"> " +
                    "and book_press like concat('%','{press}','%') </if>" +
                    "<if test=\"author != null\"> " +
                    "and book_author like concat('%','{author}','%') </if>" +
                    "order by book_borrower"+
                    "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectMyBorrowed(Book book);
}




